<template>
  <d2-container>
    <el-form :inline="true" :model="dataSearch" ref="dataSearch">
      <el-form-item label="员工姓名" prop="staff">
        <el-input v-model="dataSearch.staff" placeholder="请输入员工姓名"></el-input>
      </el-form-item>

      <el-form-item label="审批状态" prop="status">
        <el-radio-group v-model="dataSearch.status">
          <el-radio :label="-1">取消</el-radio>
          <el-radio :label="0">审批中</el-radio>
          <el-radio :label="1">审批同意</el-radio>
          <el-radio :label="2">审批拒绝</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="formSubmit">
          <d2-icon name="search" />查询
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="formReset">
          <d2-icon name="refresh" />重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-table :data="dataList" border style="width: 100%">
      <el-table-column prop="title" label="报销主题"></el-table-column>
      <el-table-column prop="account" label="报销金额"></el-table-column>
      <el-table-column prop="remark" label="报销备注"></el-table-column>
      <el-table-column prop="status" label="报销状态"></el-table-column>
      <el-table-column prop="staff" label="报销员工"></el-table-column>
      <el-table-column fixed="createdAt" label="创建时间"></el-table-column>

      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="detail(scope.row)" type="text" size="small">查看详情</el-button>
          <el-button @click="agree(scope.row)" type="text" size="small" v-show="scope.row.status == '审批中'">同意</el-button>
          <el-button @click="refuse(scope.row)" type="text" size="small" v-show="scope.row.status == '审批中'">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total,sizes,prev, pager, next,jumper"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handlerSizeChange"
      @next-click="handleNextPage"
      @prev-click="handlePrevClick"
    ></el-pagination>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import {mapState} from "vuex";

export default {
  data() {
    return {
      dataList: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      classroom: [],
      dataSearch: {
        staff:"",
        status:undefined
      }
    };
  },
  computed: {
    ...mapState('d2admin/user', ['info']),
  },
  methods: {
    formSubmit() {
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    formReset() {
      this.$refs.dataSearch.resetFields();
    },
    detail(e) {
      this.$router.push({ path: "/reimburse-detail?id=" + e._id });
    },
    agree(e) {
      this.$confirm("通过此报销申请, 是否确认?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request({
            url: "/reimburse/agreeReimburse",
            method: "post",
            data: { _id: e._id}
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "审批通过"
              });
              this.getDataList(this.dataSearch);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "未审批"
          });
        });
    },
    refuse(e) {
      this.$confirm("拒绝此报销申请, 是否确认?", "提示", {
        confirmButtonText: "拒绝",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request({
            url: "/reimburse/refuseReimburse",
            method: "post",
            data: { _id: e._id}
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "审批未通过"
              });
              this.getDataList(this.dataSearch);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "未审批"
          });
        });
    },

    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlerSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handleNextPage() {
      this.currentPage = this.currentPage + 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlePrevClick() {
      this.currentPage = this.currentPage - 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    // handleAgreeClick(e) {
    //   this.$confirm("此操作同意审批通过, 是否继续?", "提示", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   })
    //     .then(() => {
    //       request({
    //         url: "/reimburse/updateReimburse",
    //         method: "post",
    //         data: { _id: e._id,status:1 }
    //       }).then(res => {
    //         if (res.code == 200) {
    //           this.$message({
    //             message: "操作成功"
    //           });
    //           this.getDataList(this.dataSearch);
    //         }
    //       });
    //     })
    //     .catch(() => {
    //       this.$message({
    //         type: "info",
    //         message: "已取消操作"
    //       });
    //     });
    // },

    handleDisagreeClick(e) {
      this.$confirm("此操作拒绝审批通过, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
              .then(() => {
                request({
                  url: "/reimburse/updateReimburse",
                  method: "post",
                  data: { _id: e._id ,status: 2}
                }).then(res => {
                  if (res.code == 200) {
                    this.$message({
                      message: "操作成功"
                    });
                    this.getDataList(this.dataSearch);
                  }
                });
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消操作"
                });
              });
    },

    getDataList(data) {
      this.dataSearch.limitSchool = this.info.school.map(value => value._id);
      data.limitSchool = this.dataSearch.limitSchool;

      request({
        url: "/reimburse/getReimburseList",
        method: "post",
        data: data
      }).then(res => {
        this.dataList = [];
        this.dataList = res.data.reimburse;
        this.dataList.forEach((item, index) => {
          let status = parseInt(item.status);
          this.dataList[index].status =
            status === 0
              ? "审批中"
              : status === -1
              ? "已取消"
              : status === 1
              ? "审批通过"
              : status === 2
              ? "审批未通过"
              : "未知状态";
          this.dataList[index].staff = item.staff.name;
        });
        this.total = res.data.total;
        this.currentPage = res.data.currentPage;
      });
    },
    load(){
      this.school = this.info.school[0]._id
    this.dataSearch.school = this.school
    this.dataSearch.page = this.currentPage;
    this.dataSearch.pageSize = this.pageSize;
    this.getDataList(this.dataSearch);
    }
  },
  beforeRouteEnter(to, from, next) {
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                v.load()
                })
            }
            else{
                next()
            }


        },
  created() {
    this.load()
  }
};
</script>
